package io.zhengsh.juc._1cas;

public class WaitNotifyTest {
    public void testWaitMethod(Object lock) {
        try {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + " begin wait()"); // wait() 释放锁资源
                Thread.sleep(5000);
                lock.wait();
                System.out.println(Thread.currentThread().getName() + " end wait()");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testNotifyMethod(Object lock) {
        synchronized (lock) {
            System.out.println(Thread.currentThread().getName() + " begin notify()");
            try {
                Thread.sleep(5000);
                lock.notify();
                System.out.println(Thread.currentThread().getName() + " end notify()");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Object lock = new Object();
        WaitNotifyTest test = new WaitNotifyTest();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.testWaitMethod(lock);
            }
        }, "threadA");
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.testNotifyMethod(lock);
            }
        }, "threadB");
        t2.start();

        try {
            Thread.sleep(505555);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
